################################################################################
# IMPORTS
################################################################################

from django.conf import settings
from django.contrib.syndication.feeds import Feed

from taxonomy.models import Term

################################################################################
# CLASS TermFeed
################################################################################
class TermFeed(Feed):
    """
    The TermFeed class is provided to help integrating objects related to
    a given term to a RSS Feed.

    See django.contrib.syndication for information on the django
    syndication framework. Among other things, you'll find there how
    to create template files to render feeds item.
    """

    def __init__(self, term_name, title=None, *args, **kwargs):
        super(TermFeed, self).__init__(*args, **kwargs)
        # Do throw an exception when the term is not found.
        term = Term.objects.get(namefield=term_name)
        data = term.get_render_data(0)
        self.title = term.name
        self.description = term.description
        self.link = term.get_absolute_url()
        self.instances = data['instances']
        
    def items(self):
        """ returns the list of items to include in the feed """
        return self.instances

    def item_pubdate(self, item):
        """
        returns the publication date of an item

        @item: The item from which to get the publication date from
               as returned by self.items
        """
        return item['pubdate']

    def item_author_name(self, item):
        """
        returns the author name of an item

        @item: The item from which to get the author name from
               as returned by self.items
        """
        return item['author']

